<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!-- 用户id、ip、登录时间、地区、状态 -->
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="用户">
                <a-input v-model="queryParam.userName" placeholder="" />
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option v-for="it in financeStatusList" :key="it.id" :value="it.id">{{
                    it.name
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="时间">
                  <a-range-picker
                    show-time
                    v-model="queryParam.buildTime"
                    style="width: 100%"
                    :placeholder="['请选择开始日期', '请选择结束日期']"
                  />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => ((this.queryParam = {}), (this.need = {}))"
                  >重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <!-- <a-button type="primary" icon="plus" @click="handleAdd"  v-action:financeLog_add>新建</a-button> -->
        <a-button type="primary" icon="plus" @click="handleAdd">充值</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: 'auto' }"
      >
        <span slot="param" slot-scope="text">
          <div class="param-wrap">{{ text }}</div>
        </span>
        <span slot="ftype" slot-scope="text, record">
          {{ record.typeId1Name }}{{ record.typeId2Name ? ` - ${record.typeId2Name}` : '' }}
        </span>
        <span slot="atta" slot-scope="text">
          <template>
            <AWM-file-list :list="text"></AWM-file-list>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleCheckList(record)" v-action:financeAudit_list>审核记录</a>
            <a-divider type="vertical" />
            <a @click="handleEdit(record)" v-action:financeLog_update>编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDel(record)" v-action:financeLog_update>删除</a>
          </template>
        </span>
      </s-table>
      <!-- IncomeForm -->
      <sms-balance-log-add
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis, AWMFileList } from '@/components'
import { financeLogList, financeLogAdd, financeLogUpdate } from '@/api/finance'
import { getSmsBalanceLogList, getUserUpdateSmsBalance } from '@/api/sms'
import { financeStatusList, typeList, needList } from '@/utils/other'
import SmsBalanceLogAdd from './modules/SmsBalanceLogAdd'
import { Cascader } from 'ant-design-vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '用户',
    dataIndex: 'userName',
  },
  {
    title: '数量',
    dataIndex: 'number',
  },
  {
    title: '充值前余额',
    dataIndex: 'beforeNum',
  },
  {
    title: '充值后余额',
    dataIndex: 'afterNum',
  },
  {
    title: '充值时间',
    dataIndex: 'addTime',
  },
  {
    title: '充值人',
    dataIndex: 'addUserName',
  },
]

export default {
  name: 'AWM-Expend',
  components: {
    STable,
    Ellipsis,
    AWMFileList,
    SmsBalanceLogAdd,
    'a-cascader': Cascader,
  },
  data() {
    this.columns = columns
    return {
      open: false,
      need: {},
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      financeTypeList: [],
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {
      },
      data: [],
      // 状态
      financeStatusList,
      typeList,
      needList,

      // 排序
      sortJ: {
        addTime: 'add_time',
      },
      allAmount: 0,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let sortJ = this.sortJ
        if (parameter.sortField) {
          parameter.sortJ = sortJ
        }
        // 附加属性
        let subParam = {}
        const requestParameters = Object.assign({}, parameter, this.queryParam, subParam)
        return getSmsBalanceLogList(requestParameters).then((res) => {
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
    let query = this.$route.query
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
      }
    },
  },
  methods: {
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
            delete values.id
            getUserUpdateSmsBalance(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      if (!this.open && !this.advanced) {
        this.open = true
        this.getClientList('', '', true)
        this.getEpibolyList('', '', true)
        this.getFilialeList('', '', true)
        this.getProjectList('', '', true)
        this.getStudioList('', '', true)
        this.getTbList('', '', true)
      }
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date()),
      }
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '收入明细删除',
        content: `是否删除支出明细:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal),
        onCancel: () => {},
      })
    },

    handleDelete(a_id, a_modal) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: -1 }
        financeLogUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
    handleCheckList(record) {
      this.check.mdl = { ...record }
      this.check.visible = true
    },
    handleCheckCancel() {
      this.check.visible = false
    },
    // handleEdit(record){
    //   this.edit.mdl = { ...record }
    //   this.edit.visible = true
    // },
    // handleEditCancel(){
    //   this.edit.visible = false
    // },
    // handleEditOk(){

    // }
  },
}
</script>
<style scoped lang="less">
.param-wrap {
  min-width: 200px;
  max-width: 300px;
}

.a-tag-wrap {
  font-size: 12px;
  /deep/ .ant-tag {
    line-height: 18px;
    padding: 0 4px;
  }
  div {
    margin-bottom: 2px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>


 